<?php

namespace App\Models\Behavior;

use Illuminate\Database\Eloquent\Model;

class Record extends Model
{
    protected $table = 'love_behavior_records';

    const UPDATED_AT = null;

    public function img()
    {
        return $this->belongsTo(Image::Class, 'behavior_image_id');
    }

    /**
     * 获取评分记录
     *
     * @param $user_id
     * @param int $date_type
     * @return mixed
     */
    public static function getBehaviorRecords($user_id, $date_type = 0)
    {
        return static::where('user_id', $user_id)
            ->when($date_type, function ($query) use ($date_type){
                $date = get_time($date_type);
                return $query->whereBetween('created_at', [$date['start_time'], $date['end_time']]);
            })
            ->leftJoin('love_behavior_images as b','b.id','love_behavior_records.behavior_image_id')
            ->select([
                'love_behavior_records.id',
                'user_id',
                'behavior_id',
                'behavior_name',
                'behavior_type',
                'score',
                'behavior_image_id',
                'record_user_id',
                'love_behavior_records.created_at',
                'b.url as image_url'
            ])
            ->paginate(15)->toArray();

    }









    /**
     * 获取最新10条记录
     *
     * @return mixed
     */
    public static function getNewBehaviorRecords()
    {
        $data = static::leftJoin('love_users as c', 'c.id', 'love_behavior_records.record_user_id')
            ->leftJoin('love_users as d', 'd.id', 'love_behavior_records.user_id')
            ->select([
                'love_behavior_records.id',
                'user_id',
                'd.username',
                'behavior_id',
                'behavior_name',
                'score',
                'record_user_id',
                'c.username as record_user_name',
                'love_behavior_records.created_at',
            ])
            ->orderBy('love_behavior_records.created_at', 'desc')
            ->limit(10)->get();

        foreach ($data as $key => $val) {
            $data[$key]['time_tran'] = format_date(strtotime($val['created_at']));
        }

        return $data;

    }

    /**
     * 获取排行榜记录
     *
     * @return array
     */
    public static function getBehaviorRange()
    {
        $record_data = static::leftJoin('love_users as b', 'b.id', 'love_behavior_records.user_id')
            ->leftJoin('love_family_users as c', 'c.user_id', 'love_behavior_records.user_id')
            ->selectRaw('
                love_behavior_records.user_id,
                b.username,
                b.avatar,
                SUM(love_behavior_records.score) as total_score,
                (case when c.is_householder = 1 then 1 else 0 end) is_householder
            ')
            ->groupBy('love_behavior_records.user_id')
            ->orderBy('score', 'desc')
            ->paginate(10)->toArray();

        $data = [];
        if ($record_data) {
            $data['current_page'] = $record_data['current_page'];
            $data['total_page'] = $record_data['last_page'];
            $data['data'] = $record_data['data'];
        }

        return $data;
    }

    /**
     * 获取我的点评
     *
     * @param $record_user_id
     * @return mixed
     */
    public static function getMyBehaviorRecords($record_user_id)
    {
        $data['summary'] = static::where('record_user_id', $record_user_id)
            ->selectRaw('
                COUNT(DISTINCT(user_id)) user_count,
                SUM(CASE WHEN behavior_type = 1 THEN score ELSE 0 END) positive_score,
                SUM(CASE WHEN behavior_type = 2 THEN score ELSE 0 END) negative_score
            ')->get();

        $record_data = static::where('record_user_id', $record_user_id)
            ->leftJoin('love_users as b', 'b.id', 'love_behavior_records.user_id')
            ->leftJoin('love_users as c', 'c.id', 'love_behavior_records.record_user_id')
            ->leftJoin('love_behavior_images as d','d.id','love_behavior_records.behavior_image_id')
            ->selectRaw('
                love_behavior_records.*,
                d.url as behavior_image_url,
                b.username,
                b.avatar,
                c.username as record_user_name
            ')
            ->orderBy('love_behavior_records.created_at', 'desc')
            ->paginate(30)->toArray();

        $data['list'] = [];
        if ($record_data) {

            $d = [];
            foreach ($record_data['data'] as $key => $val) {
                $date = date('Y-m-d', strtotime($val['created_at']));
                $d[$date]['date'] = $date;
                $d[$date]['data'][] = $val;
            }

            $data['list']['current_page'] = $record_data['current_page'];
            $data['list']['total_page'] = $record_data['last_page'];
            $data['list']['data'] = array_values($d);
        }

        return $data;

    }

    /**
     * 获取全部点评
     *
     * @return mixed
     */
    public static function getAllBehaviorRecords()
    {
        $data['summary'] = static::selectRaw('
                COUNT(DISTINCT(user_id)) user_count,
                SUM(CASE WHEN behavior_type = 1 THEN score ELSE 0 END) positive_score,
                SUM(CASE WHEN behavior_type = 2 THEN score ELSE 0 END) negative_score
            ')->get();

        $record_data = static::leftJoin('love_users as b', 'b.id', 'love_behavior_records.user_id')
            ->leftJoin('love_users as c', 'c.id', 'love_behavior_records.record_user_id')
            ->leftJoin('love_behavior_images as d','d.id','love_behavior_records.behavior_image_id')
            ->selectRaw('
                love_behavior_records.*,
                d.url as behavior_image_url,
                b.username,
                b.avatar,
                c.username as record_user_name
            ')
            ->orderBy('love_behavior_records.created_at', 'desc')
            ->paginate(30)->toArray();

        $data['list'] = [];
        if ($record_data) {

            $d = [];
            foreach ($record_data['data'] as $key => $val) {
                $date = date('Y-m-d', strtotime($val['created_at']));
                $d[$date]['date'] = $date;
                $d[$date]['data'][] = $val;
            }

            $data['list']['current_page'] = $record_data['current_page'];
            $data['list']['total_page'] = $record_data['last_page'];
            $data['list']['data'] = array_values($d);
        }

        return $data;

    }

    /**
     * 获取点评
     *
     * @param int $record_user_id
     * @return mixed
     */
    public static function getRecords($record_user_id = 0)
    {
        $data['summary'] = static::when($record_user_id, function ($query) use ($record_user_id){
                return $query->where('record_user_id', $record_user_id);
            })
            ->selectRaw('
                COUNT(DISTINCT(user_id)) user_count,
                SUM(CASE WHEN behavior_type = 1 THEN score ELSE 0 END) positive_score,
                SUM(CASE WHEN behavior_type = 2 THEN score ELSE 0 END) negative_score
            ')->first();

        $data['summary']['user_count'] = $data['summary']['user_count'] ? $data['summary']['user_count'] : 0;
        $data['summary']['positive_score'] = $data['summary']['positive_score'] ? $data['summary']['positive_score'] : 0;
        $data['summary']['negative_score'] = $data['summary']['negative_score'] ? $data['summary']['negative_score'] : 0;

        $record_data = static::when($record_user_id, function ($query) use ($record_user_id){
                return $query->where('record_user_id', $record_user_id);
            })
            ->leftJoin('love_users as b', 'b.id', 'love_behavior_records.user_id')
            ->leftJoin('love_users as c', 'c.id', 'love_behavior_records.record_user_id')
            ->leftJoin('love_behavior_images as d','d.id','love_behavior_records.behavior_image_id')
            ->selectRaw('
                love_behavior_records.*,
                d.url as behavior_image_url,
                b.username,
                b.avatar,
                c.username as record_user_name
            ')
            ->orderBy('love_behavior_records.created_at', 'desc')
            ->paginate(30)->toArray();

        $data['list'] = [];
        if ($record_data) {

            $d = [];
            foreach ($record_data['data'] as $key => $val) {
                $date = date('Y-m-d', strtotime($val['created_at']));
                $d[$date]['date'] = $date;
                $d[$date]['data'][] = $val;
            }

            $data['list']['current_page'] = $record_data['current_page'];
            $data['list']['total_page'] = $record_data['last_page'];
            $data['list']['data'] = array_values($d);
        }

        return $data;

    }

    /**
     * 根据user_id 获取个人记录
     *
     * @param int $record_user_id
     * @return mixed
     */
    public static function getRecordsByUserId($user_id = 0)
    {
        $record_data = static::where('user_id', $user_id)
            ->leftJoin('love_users as c', 'c.id', 'love_behavior_records.record_user_id')
            ->leftJoin('love_behavior_images as d','d.id','love_behavior_records.behavior_image_id')
            ->selectRaw('
                love_behavior_records.*,
                d.url as behavior_image_url,
                c.username as record_user_name
            ')
            ->orderBy('love_behavior_records.created_at', 'desc')
            ->paginate(30)->toArray();

        $data = [];
        if ($record_data) {

            $d = [];
            foreach ($record_data['data'] as $key => $val) {
                $date = date('Y-m-d', strtotime($val['created_at']));
                $d[$date]['date'] = $date;
                $d[$date]['data'][] = $val;
            }

            $data['current_page'] = $record_data['current_page'];
            $data['total_page'] = $record_data['last_page'];
            $data['data'] = array_values($d);
        }

        return $data;

    }


}
